import { history } from 'umi';
import { setToken } from '@/utils/authority';

export default {
  namespace: 'login',
  state: {},
  effects: {
    *login({ payload }) {
      setToken(payload);
      history.replace('/');
    },
    *logout(__, { put }) {
      yield put({ type: 'user/clear' });
      setToken('');
      localStorage.setItem('feedKey', JSON.stringify([]));
      // 变量清空
      const htmlDom = window.document.querySelector('html');
      htmlDom.removeAttribute('style');
      if (window.location.pathname !== '/user/login') {
        history.replace({
          pathname: '/user/login',
        });
      }
    },
  },
};
